package com.qiankan.qmsj.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import com.qiankan.qmsj.R;
import com.qiankan.qmsj.UserInfo;
import com.qiankan.qmsj.db.UserDBHelper;
import com.qiankan.qmsj.entity.Product;
import com.qiankan.qmsj.entity.Result;
import com.qiankan.qmsj.net.APIHub;
import com.qiankan.qmsj.widget.TipToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddCommentActivity extends BaseActivity {
    @BindView(R.id.rb_experience)
    RatingBar mRbExperience;
    @BindView(R.id.rb_rate)
    RatingBar mRbRate;
    @BindView(R.id.rb_speed)
    RatingBar mRbSpeed;
    @BindView(R.id.tv_stars_experience)
    TextView mTvExpericece;
    @BindView(R.id.tv_stars_speed)
    TextView mTvSpeed;
    @BindView(R.id.tv_rate)
    TextView mTvRate;
    @BindView(R.id.et_loan_money)
    EditText mEtLoanMoney;
    @BindView(R.id.et_comment)
    EditText mEtComment;
    @BindView(R.id.rg_root)
    RadioGroup mRadioGroup;
    private UserInfo userInfo;
    private Product product;
    private String status = "借款成功";

    public static Intent newIntent(Context context, Product product) {
        Intent intent = new Intent(context, AddCommentActivity.class);
        intent.putExtra("product", product);
        return intent;
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_add_comment;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        userInfo = UserDBHelper.getInstance(this).getUserInfo();
        product = (Product) getIntent().getSerializableExtra("product");
        if (product == null) {
            return;
        }
        mRbExperience.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                mTvExpericece.setText(String.format("%1$s分", v));
            }
        });
        mRbSpeed.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                mTvSpeed.setText(String.format("%1$s分", v));
            }
        });
        mRbRate.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                mTvRate.setText(String.format("%1$s分", v));
            }
        });
    }

    @OnCheckedChanged(R.id.rb_1)
    public void onCheckedSuccess(CompoundButton compoundButton, boolean b) {
        if (b) {
            status = "借款成功";
        }
    }

    @OnCheckedChanged(R.id.rb_2)
    public void onCheckedFail(CompoundButton compoundButton, boolean b) {
        if (b) {
            status = "借款失败";
        }
    }

    @OnCheckedChanged(R.id.rb_3)
    public void onCheckedOther(CompoundButton compoundButton, boolean b) {
        if (b) {
            status = "其他";
        }
    }

    @OnClick(R.id.btn_addComment)
    public void onClickAddComment(View view) {
        if (checkSave()) {
            Map<String, String> param = new HashMap<String, String>();
            param.put("user", userInfo.getId());
            param.put("productId", product.getId());
            param.put("content", mEtComment.getText().toString());
//            param.put("satisfaction", "");
            param.put("experience", mRbExperience.getRating() + "");
            param.put("undersigned", mRbSpeed.getRating() + "");
            param.put("ratesign", mRbRate.getRating() + "");
            param.put("money", mEtLoanMoney.getText().toString());
            param.put("status", status);
            showLoadingDialog();
            APIHub.getsInstance().getBaseService().addComment(param).enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    dismissLoadingDialog();
                    Result result = response.body();
                    if (result.getCode().equals("200")) {
                        TipToast.toast(AddCommentActivity.this, "评论成功!");
                        finish();
                    } else {
                        TipToast.toast(AddCommentActivity.this, result.getRemark());
                    }
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    dismissLoadingDialog();
                    TipToast.toast(AddCommentActivity.this, "网络连接失败");
                }
            });
        }
    }

    private boolean checkSave() {
        String loanMoney = mEtLoanMoney.getText().toString();
        String comment = mEtComment.getText().toString();
        if (TextUtils.isEmpty(loanMoney)) {
            TipToast.toast(this, "请输入借款金额");
            return false;
        }
        if (TextUtils.isEmpty(comment)) {
            TipToast.toast(this, "请输入评论内容");
            return false;
        }
        return true;
    }

    @OnClick(R.id.iv_back)
    public void onClickBack(View view) {
        finish();
    }
}
